package com.css.fxfzxczh.modules.lossAssessment.floorArea.service.impl;

import cn.hutool.core.lang.hash.Hash;
import com.alibaba.druid.util.StringUtils;
import com.css.fxfzxczh.base.dict.service.DictService;
import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.lossAssessment.floorArea.entity.FloorArea;
import com.css.fxfzxczh.modules.lossAssessment.floorArea.parm.FloorAreaParam;
import com.css.fxfzxczh.modules.lossAssessment.floorArea.repository.FloorAreaRepository;
import com.css.fxfzxczh.modules.lossAssessment.floorArea.service.FloorAreaService;
import com.css.fxfzxczh.util.CreateGson;
import com.css.fxfzxczh.util.PlatformDateUtils;
import com.css.fxfzxczh.web.PlatformSessionContext;
import com.css.fxfzxczh.zcpt.sys.entity.SUser;
import com.css.fxfzxczh.zcpt.sys.service.SUserService;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.locationtech.jts.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import static com.css.fxfzxczh.util.PoiExcelUtil.getWorkBook;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/23
 * @Description: 确定总建筑面积
 */
@Service
public class FloorAreaServiceImpl implements FloorAreaService {

    @Resource
    private FloorAreaRepository floorAreaRepository;
    @Resource
    SUserService sUserService;
    @Resource
    DictService dictService;

    //分页查询
    @Override
    public Map<String, Object> getPage(int curPage, int pageSize, FloorAreaParam param) {
        return floorAreaRepository.getPage(curPage,pageSize,param);
    }

    //导入
    @Override
    public String reportImport(InputStream file, String xsemId) {
        try {
            Workbook workbook = WorkbookFactory.create(file);
            Map<String, Object> map = getList(workbook, xsemId);
            Object o = map.get("message");
            if (o != null) {
                return String.valueOf(o);
            } else {
                List<FloorArea> list= (List<FloorArea>)map.get("list");
                floorAreaRepository.batchSave(list);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "导入成功";
    }

    //删除
    @Override
    public void deleteById(List<String> id) {
        floorAreaRepository.deleteById(id);
    }

    //根据id更新
    @Override
    public void updateById(FloorArea param) {
        param.setUpdateUser(PlatformSessionContext.getUserID());
        param.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        floorAreaRepository.updateById(param);
    }

    //根据id查询
    @Override
    public FloorArea findById(String id) {
        return floorAreaRepository.findById(id);
    }
    //获取评估子区下拉
    @Override
    public List<String> getEvaluationSubarea(String xsemId) {
        List<String> list =floorAreaRepository.getEvaluationSubarea(xsemId);
        return list;
    }

    //获取list
    private HashMap<String,Object> getList(Workbook workbook, String xsemId) {
        String userID = PlatformSessionContext.getUserID();
        String user = sUserService.getSUser(userID);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        HashMap<String, Object> map = new HashMap<>();
        //构筑物部分
        List<FloorArea> list = new ArrayList<>();
        if (workbook != null) {
            //循环除了第一行的所有行
            /** 得到第二个sheet */
            Sheet sheet = workbook.getSheetAt(0);
            /** 得到Excel的行数 */
            int totalRows = sheet.getPhysicalNumberOfRows();
            HSSFDataFormatter hSSFDataFormatter = new HSSFDataFormatter();
            /** 循环Excel的行 从第七行开始*/
            for (int r = 2; r < totalRows; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                //查询是否有该评估子区
                String name = floorAreaRepository.getName(hSSFDataFormatter.formatCellValue(row.getCell(0)));
                //如果没有则禁止导入
                if (StringUtils.isEmpty(name)){
                    map.put("message","没有该评估子区: "+hSSFDataFormatter.formatCellValue(row.getCell(0)));
                    return map;
                }
                FloorArea floorArea= new FloorArea();
                //主键id
                floorArea.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                //地震事件id
                floorArea.setXsemId(xsemId);
                //评估子区
                floorArea.setArea(hSSFDataFormatter.formatCellValue(row.getCell(0)));
                //结构类型
                floorArea.setStructureType(dictService.getKeyByDictCodeAndValue("jglx", hSSFDataFormatter.formatCellValue(row.getCell(1))));
                //总建筑面积
                floorArea.setGrossFloorArea(hSSFDataFormatter.formatCellValue(row.getCell(2)));
                //上报人
                floorArea.setReporter(sUser.getUserName());
                //上报时间
                floorArea.setReporterTime(PlatformDateUtils.getCurrentTimestamp());
                //创建人
                floorArea.setCreateUser(userID);
                //0正常1删除
                floorArea.setDelFlag(YNEnum.N.toString());
                //创建事件
                floorArea.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                list.add(floorArea);
            }
        }
        map.put("list",list);
        return map;
    }
}
